﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
using Tools;
using BLL;

namespace 下载写号
{
    public class DownloadAndWriteFormBLL
    {
        private SimbaBLL simbaBLL = new SimbaBLL();
        private DownloadBLL downloadBLL = new DownloadBLL();
        private delegate void invokehandler();
        public void SetPorts(Setting set, int num, AppHelper app, DownloadAndWriteForm form)
        {
            List<string> ports = new List<string>();
            if (num == 4)
            {
                ports.Add(set.port1);
                ports.Add(set.port2);
                ports.Add(set.port3);
                ports.Add(set.port4);
            }
            else
            {
                ports.Add(set.port5);
                ports.Add(set.port6);
                ports.Add(set.port7);
                ports.Add(set.port8);
            }
            AppHelper.Delay(500);
            app.MouseClick(PointModel.Simba_XH.Set);
            AppHelper.Delay(800);
            form.Invoke(new invokehandler(delegate ()
            {
                simbaBLL.SetPort(PointModel.Simba_XH.Port1, app, ports[0]);
                simbaBLL.SetPort(PointModel.Simba_XH.Port2, app, ports[1]);
                simbaBLL.SetPort(PointModel.Simba_XH.Port3, app, ports[2]);
                simbaBLL.SetPort(PointModel.Simba_XH.Port4, app, ports[3]);
            }));
            AppHelper.Delay(800);
            app.MouseClick(PointModel.Simba_XH.Apply);
            AppHelper.Delay(100);
            app.MouseClick(PointModel.Simba_XH.Apply);
            AppHelper.Delay(100);
        }
        public void StarWrite(List<string> codeList, AppHelper app,int num, SerialPort relayPort, PortBLL portBLL, DownloadAndWriteForm form)
        {
            app.MouseClick(PointModel.Simba_XH.Home);
            AppHelper.Delay(100);
            app.MouseClick(PointModel.Simba_XH.Home);
            app.MouseClick(PointModel.Simba_XH.Start);
            AppHelper.Delay(900);
            for (int i = 0; i < codeList.Count; i++)
            {
                AppHelper.Delay(900);
                form.Invoke(new invokehandler(delegate ()
                {
                    Clipboard.SetText(codeList[i]);
                }));
                app.KeyDown(Keys.ControlKey, Keys.V);
                AppHelper.Delay(900);
                app.KeyDown(Keys.Enter);
                switch (i)
                {
                    case 0: if (num == 4) portBLL.SendData(relayPort, PortModel.PowerON_1); else portBLL.SendData(relayPort, PortModel.PowerON_5); break;
                    case 1: if (num == 4) portBLL.SendData(relayPort, PortModel.PowerON_2); else portBLL.SendData(relayPort, PortModel.PowerON_6); break;
                    case 2: if (num == 4) portBLL.SendData(relayPort, PortModel.PowerON_3); else portBLL.SendData(relayPort, PortModel.PowerON_7); break;
                    case 3: if (num == 4) portBLL.SendData(relayPort, PortModel.PowerON_4); else portBLL.SendData(relayPort, PortModel.PowerON_8); break;
                    default:
                        break;
                }
            }
        }
        public void WaitDownload(int Second)
        {
            List<string> result = new List<string>();
            while (Second > 0)
            {
                downloadBLL.SubPicture();
                string Pngstr = downloadBLL.ImageToText(System.Environment.CurrentDirectory + @"\F_IMG\imgtp.png");
                string[] downloadResult = Pngstr.Split(new char[] { '\r', '\n' });
                result.Clear();
                foreach (var item in downloadResult)
                {
                    if (item != "")
                    {
                        result.Add(item);
                    }
                }
                if (result.Count == 8)
                {
                    if ((result[0].Contains("Passed") || result[0].Contains("Failed")) &&
                        (result[1].Contains("Passed") || result[1].Contains("Failed")) &&
                        (result[2].Contains("Passed") || result[2].Contains("Failed")) &&
                        (result[3].Contains("Passed") || result[3].Contains("Failed")) &&
                        (result[4].Contains("Passed") || result[4].Contains("Failed")) &&
                        (result[5].Contains("Passed") || result[5].Contains("Failed")) &&
                        (result[6].Contains("Passed") || result[6].Contains("Failed")) &&
                        (result[7].Contains("Passed") || result[7].Contains("Failed")))
                    {
                        break;
                    }
                }
                AppHelper.Delay(100);
                Second--;
            }
        }
        public void GetWriteReslut(AppHelper app, ref List<string> result, SerialPort relayPort, PortBLL portBLL)
        {
            List<string> Status = new List<string>() { "ng", "ng", "ng", "ng" };
            var t1 = Task.Factory.StartNew(() => Status[0] = simbaBLL.IsPass(ResultModel.WriteModel.PCB1, app));
            var t2 = Task.Factory.StartNew(() => Status[1] = simbaBLL.IsPass(ResultModel.WriteModel.PCB2, app));
            var t3 = Task.Factory.StartNew(() => Status[2] = simbaBLL.IsPass(ResultModel.WriteModel.PCB3, app));
            var t4 = Task.Factory.StartNew(() => Status[3] = simbaBLL.IsPass(ResultModel.WriteModel.PCB4, app));
            Task.WaitAll(t1, t2, t3, t4);
            portBLL.SendData(relayPort, PortModel.PowerOFF);
            if (Status[0] == "ng" || Status[1] == "ng" || Status[2] == "ng" || Status[3] == "ng")
            {
                if (Status[0] == "ng")
                {
                    AppHelper.Delay(100);
                    if (result.Count == 0)
                    {
                        portBLL.SendData(relayPort, PortModel.PowerON_1);
                    }
                    else
                    {
                        portBLL.SendData(relayPort, PortModel.PowerON_5);
                    }
                    t1 = Task.Factory.StartNew(() => Status[0] = simbaBLL.IsPass(ResultModel.WriteModel.PCB1, app));
                }
                if (Status[1] == "ng")
                {
                    AppHelper.Delay(100);
                    if (result.Count == 0)
                    {
                        portBLL.SendData(relayPort, PortModel.PowerON_2);
                    }
                    else
                    {
                        portBLL.SendData(relayPort, PortModel.PowerON_6);
                    }
                    t2 = Task.Factory.StartNew(() => Status[1] = simbaBLL.IsPass(ResultModel.WriteModel.PCB2, app));
                }
                if (Status[2] == "ng")
                {
                    AppHelper.Delay(100);
                    if (result.Count == 0)
                    {
                        portBLL.SendData(relayPort, PortModel.PowerON_3);
                    }
                    else
                    {
                        portBLL.SendData(relayPort, PortModel.PowerON_7);
                    }
                    t3 = Task.Factory.StartNew(() => Status[2] = simbaBLL.IsPass(ResultModel.WriteModel.PCB3, app));
                }
                if (Status[3] == "ng")
                {
                    AppHelper.Delay(100);
                    if (result.Count == 0)
                    {
                        portBLL.SendData(relayPort, PortModel.PowerON_4);
                    }
                    else
                    {;
                        portBLL.SendData(relayPort, PortModel.PowerON_8);
                    }
                    t4 = Task.Factory.StartNew(() => Status[3] = simbaBLL.IsPass(ResultModel.WriteModel.PCB4, app));
                }
                Task.WaitAll(t1, t2, t3, t4);
            }
            if (Status[0] == "ng")
            {
                app.MouseClick(PointModel.Simba_XH.Stop1);
            }
            if (Status[1] == "ng")
            {
                app.MouseClick(PointModel.Simba_XH.Stop2);
            }
            if (Status[2] == "ng")
            {
                app.MouseClick(PointModel.Simba_XH.Stop3);
            }
            if (Status[3] == "ng")
            {
                app.MouseClick(PointModel.Simba_XH.Stop4);
            }
            result.Add(Status[0]);
            result.Add(Status[1]);
            result.Add(Status[2]);
            result.Add(Status[3]);
        }
        public List<string> ResultList(List<string>result, List<Sql.t_CMCC_ProdSN> prodList)
        {
            string time = DateTime.Now.ToString("yyyyMMddHHmmsss");
            List<string> results;
            if (prodList.Count==8)
            {
                 results = new List<string>()
                {
                    "result#"+ result[7]+"#"+time+"#1#"+prodList[3].snimei,
                    "result#"+ result[6]+"#"+time+"#2#"+prodList[2].snimei,
                    "result#"+ result[5]+"#"+time+"#3#"+prodList[1].snimei,
                    "result#"+ result[4]+"#"+time+"#4#"+prodList[0].snimei,
                    "result#"+ result[3]+"#"+time+"#5#"+prodList[7].snimei,
                    "result#"+ result[2]+"#"+time+"#6#"+prodList[6].snimei,
                    "result#"+ result[1]+"#"+time+"#7#"+prodList[5].snimei,
                    "result#"+ result[0]+"#"+time+"#8#"+prodList[4].snimei,
                };
            }
            else
            {
                results = new List<string>()
                {
                    "result#"+ result[7]+"#"+time+"#1#"+"?1",
                    "result#"+ result[6]+"#"+time+"#2#"+"?2",
                    "result#"+ result[5]+"#"+time+"#3#"+"?3",
                    "result#"+ result[4]+"#"+time+"#4#"+"?4",
                    "result#"+ result[3]+"#"+time+"#5#"+"?5",
                    "result#"+ result[2]+"#"+time+"#6#"+"?6",
                    "result#"+ result[1]+"#"+time+"#7#"+"?7",
                    "result#"+ result[0]+"#"+time+"#8#"+"?8",
                    "err: " +  prodList[0].snimei,
                };
            }
            return results;
        }
    }
}
